local nixin = fk.CreateSkill {

  name = "bkmz__nixin",
  tags = {Skill.Compulsory},
}

Fk:loadTranslationTable{
  ["bkmz__nixin"] = "拟形",
  [":bkmz__nixin"] = "锁定技，每轮开始时，你可以选择一名其他角色，进行变身（若其有副将，则你可选择其主将或副将），然后你可以获得其一个技能。当你发动以此法获得的最新的技能时（状态技和法则技除外，如咆哮），被你拟形过的角色失去1点体力。",
  ["#bkmz__nixin-choose"] = "拟形：你可以选择一名其他角色",
  ["#bkmz__nixin-general"] = "拟形：你可以选择 %src 的主将或副将进行变身",
  ["#bkmz__nixin_copy"] = "拟形：请选择要获得的技能",
  ["#bkmz__nixin_get_skill"] = "%from 通过【拟形】获得了 %to 的【%arg】技能",
  ["#bkmz__nixin_trigger"] = "%from 发动了通过【拟形】获得的【%arg】技能，%to 失去了1点体力",
  ["mainGeneral"] = "主将",
  ["deputyGeneral"] = "副将",
}


nixin:addEffect(fk.RoundStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(nixin.name) and not player.dead and table.find(player.room:getOtherPlayers(player), function(p) return not p.dead end)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:askToChoosePlayers(player, {
      targets = room:getOtherPlayers(player),
      min_num = 1,
      max_num = 1,
      prompt = "#bkmz__nixin-choose",
      skill_name = nixin.name,
      cancelable = true,
    })
    if #to == 0 then return false end
    local choicep = {"mainGeneral"}
    local skills = {}
    if to[1].deputyGeneral ~= "" then
      table.insert(choicep, "deputyGeneral")
    end
    local chosenp = room:askToChoice(player, {
      choices = choicep,
      skill_name = nixin.name,
      prompt = "#bkmz__nixin-general:"..to[1].id,
      cancelable = false,
    })
    -- for _, s in ipairs(to[1].player_skills) do
    --   if s:isPlayerSkill(to[1]) and s.visible and not player:hasSkill(s.name, true) then
    --     table.insertIfNeed(skills, s.name)
    --   end
    -- end
    if chosenp == "mainGeneral" then
      player.general = to[1].general
      local skills1 = Fk.generals[to[1].general]:getSkillNameList(true)
      for _, s in ipairs(skills1) do
        table.insertIfNeed(skills, s)
      end
    else
      player.general = to[1].deputyGeneral
      local skills2 = Fk.generals[to[1].deputyGeneral]:getSkillNameList(true)
      for _, s in ipairs(skills2) do
        table.insertIfNeed(skills, s)
      end
    end
    if #skills > 0 then
      room:broadcastProperty(player, "general")
      local chosen = room:askToChoice(player, {
        choices = skills,
        skill_name = nixin.name,
        prompt = "#bkmz__nixin_copy",
        detailed = true,
      })
      if chosen then
        room:handleAddLoseSkills(player, chosen, nil)
        room:doIndicate(player, {to[1]})
        room:setPlayerMark(player, "bkmz__nixin_skill", chosen)
        room:setPlayerMark(to[1], "bkmz__nixin_copied_by", 1)
        room:sendLog{
          type = "#bkmz__nixin_get_skill",
          from = player.id,
          to = {to[1].id},
          arg = chosen,
        }
      end
    end
  end,
})

nixin:addEffect(fk.AfterSkillEffect, {
  name = "#bkmz__nixin_trigger",
  mute = true,
  events = {fk.AfterSkillEffect},
  can_trigger = function(self, event, target, player, data)
    if not (target == player and player:getMark("bkmz__nixin_skill") ~= 0) then
      return false
    end
    return player:hasSkill(data.skill, true) and player:getMark("bkmz__nixin_skill") == data.skill.name
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local skill_name = player:getMark("bkmz__nixin_skill")
    for _, p in ipairs(player.room.alive_players) do
        if p:getMark("bkmz__nixin_copied_by") == 1 then
            room:doIndicate(player, {p})
            room:loseHp(p, 1, "bkmz__nixin")
        end
    end
  end,
})

return nixin